package org.example.Server.context.question;

import org.example.Server.mapper.question.QuestionMapper;
import org.example.Server.model.question.QUpdate;
import org.example.Server.strategy.question.QnaQsUpdateStrategy;
import org.example.Server.strategy.question.QsUpdateStrategy;
import org.example.Server.strategy.question.SelQsUpdateStrategy;
import org.example.Server.strategy.question.YorNQsUpdateStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class QsUpdateContext {
    @Autowired
    private QuestionMapper questionMapper;

//    public QsUpdateContext(QuestionMapper questionMapper) {
//        this.questionMapper = questionMapper;
//    }

    /**
     * 按照问题类型获取该环境类的对应策略
     *
     * @param qUpdate        获取问题类型
     * @param questionMapper
     * @return
     */
    private QsUpdateStrategy getStrategy(QUpdate qUpdate, QuestionMapper questionMapper) {
        String qsType = qUpdate.getQs_type();
        QsUpdateStrategy strategy = null;
        switch (qsType) {
            case "qna":
                strategy = new QnaQsUpdateStrategy(questionMapper);
                break;
            case "yesorno":
                strategy = new YorNQsUpdateStrategy(questionMapper);
                break;
            default:
                strategy = new SelQsUpdateStrategy(questionMapper);
                break;
        }
        return strategy;
    }

    public void QsUpdate(QUpdate qUpdate) {
        try {
            questionMapper.updateQs(qUpdate);
            this.getStrategy(qUpdate, this.questionMapper).qsUpdate(qUpdate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
